package com.wudi.estate.service.impl.security;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wudi.estate.bean.admin.Admin;
import com.wudi.estate.bean.admin.Role;
import com.wudi.estate.mapper.admin.AdminMapper;
import com.wudi.estate.mapper.admin.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        adminQueryWrapper.eq("username",username);
        Admin admin = adminMapper.selectOne(adminQueryWrapper);
        if(admin==null){
            throw  new UsernameNotFoundException("用户名为空");
        }
        User user=null;
//        roleQueryWrapper.select("DISTINCT name");
        roleQueryWrapper.eq("aid",admin.getId());
        List<Role> roles = roleMapper.selectList(roleQueryWrapper);
        for (Role role : roles) {
            System.out.println(role.getName());
            if("小区管理员".equals(role.getName())){
                List<GrantedAuthority> auth = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_小区管理员");
                System.out.println("auth = " + auth);
                user =new User(admin.getUsername(),new BCryptPasswordEncoder().encode(admin.getPassword()),auth);
                break;
            }
            else if("房产管理员".equals(role.getName())){
                List<GrantedAuthority> auth = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_房产管理员");
                System.out.println("auth = " + auth);
                user =new User(admin.getUsername(),new BCryptPasswordEncoder().encode(admin.getPassword()),auth);
                break;
            }
            else if("人员管理员".equals(role.getName())||"收费管理员".equals(role.getName())){
                List<GrantedAuthority> auth = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_人员管理员,ROLE_收费管理员");
                System.out.println("auth = " + auth);
                user =new User(admin.getUsername(),new BCryptPasswordEncoder().encode(admin.getPassword()),auth);
                break;
            }
            else if("车位管理员".equals(role.getName())){
                List<GrantedAuthority> auth = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_车位管理员");
                System.out.println("auth = " + auth);
                user =new User(admin.getUsername(),new BCryptPasswordEncoder().encode(admin.getPassword()),auth);
                break;
            }
            else if("服务管理员".equals(role.getName())){
                List<GrantedAuthority> auth = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_服务管理员");
                System.out.println("auth = " + auth);
                user =new User(admin.getUsername(),new BCryptPasswordEncoder().encode(admin.getPassword()),auth);
                break;
            }
            else if("资产管理员".equals(role.getName())){
                List<GrantedAuthority> auth = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_资产管理员");
                System.out.println("auth = " + auth);
                user =new User(admin.getUsername(),new BCryptPasswordEncoder().encode(admin.getPassword()),auth);
                break;
            }
//           else if("系统管理员".equals(role.getName())){
//                List<GrantedAuthority> auth = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_系统管理员");
//                System.out.println("auth = " + auth);
//                return new User(admin.getUsername(),new BCryptPasswordEncoder().encode(admin.getPassword()),auth);
//            }
            else {
//                List<GrantedAuthority> auth = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_系统管理员");
//                List<GrantedAuthority> auth = AuthorityUtils.createAuthorityList("ROLE_" + role.getName());
                List<GrantedAuthority> auth = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_"+role.getName());
                System.out.println("auth = " + auth);
                user =new User(admin.getUsername(),new BCryptPasswordEncoder().encode(admin.getPassword()),auth);
                break;
            }
        }
        return user;
    }
}
